<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>工作台<small>菜单管理</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>工作台</el-breadcrumb-item>
            <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="菜单编码/菜单名称" v-model="pagination.queryString" style="width: 200px;" class="filter-item"></el-input>
                <el-button @click="handleCurrentChange(1)" class="dalfBut">查询</el-button>
                <!--给新建按钮绑定@click事件 显示新建表单-->
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="linkUrl" label="菜单编码" align="center"></el-table-column>
                <el-table-column prop="name" label="菜单名称" align="center"></el-table-column>
                <el-table-column prop="path" label="路由路径" align="center"></el-table-column>
                <el-table-column prop="priority" label="优先级" align="center"></el-table-column>
                <el-table-column prop="description" label="描述" align="center"></el-table-column>
                <el-table-column prop="icon" label="图标" align="center"></el-table-column>
                <el-table-column prop="parentMenuId" label="父菜单id" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <!--scope为当前行的值-->
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <!--dialog弹窗可视化   设置idalogfromvisble得值表达是否可见.-->
                <el-dialog title="新增菜单项" :visible.sync="dialogFormVisible">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="菜单名称" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="访问路径" prop="linkUrl">
                                    <el-input v-model="formData.linkUrl"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="路由路径" prop="path">
                                    <el-input v-model="formData.path"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="优先级" prop="priority">
                                    <el-input v-model="formData.priority"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="图标" >
                                    <el-input v-model="formData.icon"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="父菜单id" >
                                    <el-input v-model="formData.parentMenuId"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="层级" >
                                    <el-input v-model="formData.level"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="描述">
                                    <el-input v-model="formData.description" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                            </el-tab-pane>
                    <el-tab-pane label="角色信息" name="second">
                        <div class="checkScrol">
                            <table class="datatable">
                                <thead>
                                <tr>
                                    <th>选择</th>
                                    <th>角色名称</th>
                                    <th>角色关键字</th>
                                    <th>描述</th>
                                </tr>
                                </thead>
                                <tbody>
                                <tr v-for="c in tableData">
                                    <td>
                                        <input :id="c.keyword" v-model="checkitemIds" type="checkbox" :value="c.id">
                                    </td>
                                    <td><label :for="c.keyword">{{c.name}}</label></td>
                                    <td><label :for="c.keyword">{{c.keyword}}</label></td>
                                    <td><label :for="c.keyword">{{c.description}}</label></td>
                                </tr>
                                </tbody>
                            </table>
                        </div>
                    </el-tab-pane>
                            <el-tab-pane label="子菜单信息" name="third">
                                <div class="checkScrol">
                                    <table class="datatable1">
                                        <thead>
                                        <tr>
                                            <th>选择</th>
                                            <th>菜单名称</th>
                                            <th>访问路径</th>
                                            <th>描述</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <tr v-for="c in tableData1">
                                            <td>
                                                <input :id="c.id" v-model="checkitemIds1" type="checkbox" :value="c.id">
                                            </td>
                                            <td><label :for="c.id">{{c.name}}</label></td>
                                            <td><label :for="c.id">{{c.linkUrl}}</label></td>
                                            <td><label :for="c.id">{{c.description}}</label></td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <!--dialog弹窗可视化   设置idalogfromvisble得值表达是否可见.-->
                <el-dialog title="编辑菜单项" :visible.sync="dialogFormVisibleEdit">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="菜单名称" prop="name">
                                                <el-input v-model="formData.name"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="访问路径" prop="linkUrl">
                                                <el-input v-model="formData.linkUrl"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="路由路径" prop="path">
                                                <el-input v-model="formData.path"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="优先级" prop="priority">
                                                <el-input v-model="formData.priority"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="图标" >
                                                <el-input v-model="formData.icon"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="父菜单id" >
                                                <el-input v-model="formData.parentMenuId"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="层级" >
                                                <el-input v-model="formData.level"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="描述">
                                                <el-input v-model="formData.description" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>
                            <el-tab-pane label="角色信息" name="second">
                                <div class="checkScrol">
                                    <table class="datatable">
                                        <thead>
                                        <tr>
                                            <th>选择</th>
                                            <th>角色名称</th>
                                            <th>角色关键字</th>
                                            <th>描述</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <tr v-for="c in tableData">
                                            <td>
                                                <input :id="c.keyword" v-model="checkitemIds" type="checkbox" :value="c.id">
                                            </td>
                                            <td><label :for="c.keyword">{{c.name}}</label></td>
                                            <td><label :for="c.keyword">{{c.keyword}}</label></td>
                                            <td><label :for="c.keyword">{{c.description}}</label></td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </el-tab-pane>
                            <el-tab-pane label="子菜单信息" name="third">
                                <div class="checkScrol">
                                    <table class="datatable1">
                                        <thead>
                                        <tr>
                                            <th>选择</th>
                                            <th>菜单名称</th>
                                            <th>访问路径</th>
                                            <th>描述</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <tr v-for="c in tableData1">
                                            <td>
                                                <input :id="c.id" v-model="checkitemIds1" type="checkbox" :value="c.id">
                                            </td>
                                            <td><label :for="c.id">{{c.name}}</label></td>
                                            <td><label :for="c.id">{{c.linkUrl}}</label></td>
                                            <td><label :for="c.id">{{c.description}}</label></td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisibleEdit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script>
    var vue = new Vue({
        el: '#app',
        data:{
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize:10,//每页显示的记录数
                total:0,//总记录数
                queryString:null//查询条件
            },
            dataList: [],//当前页要展示的分页列表数据
            formData: {},//表单数据
            tableData: [],//新增和编辑表单中对应的检查项列表数据
            checkitemIds: [],//新增和编辑表单中检查项对应的复选框，基于双向绑定可以进行回显和数据提交
            tableData1: [],//新增和编辑表单中对应的检查项列表数据
            checkitemIds1: [],//新增和编辑表单中检查项对应的复选框，基于双向绑定可以进行回显和数据提交
            dialogFormVisible: false,//增加表单是否可见
            dialogFormVisibleEdit: false,//编辑表单是否可见
            rules: {//校验规则
                code: [{ required: true, message: '项目编码为必填项', trigger: 'blur' }],
                name: [{ required: true, message: '项目名称为必填项', trigger: 'blur' }]
            }
        },
        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            this.findPage();
        },
        methods: {
            //编辑
            handleEdit() {
                //引用表单 通过validate方法判断表单校验是否成功
                this.$refs['dataEditForm'].validate((valid)=>{
                    //如果表单校验成功  发送ajax请求 提交表单数据,接受返回值
                    if(valid){
                        axios.post("/menu/edit.do?checkitemIds="+this.checkitemIds+"&checkitemIds1="+this.checkitemIds1,this.formData).then((response)=>{
                            //隐藏编辑窗口
                            this.dialogFormVisibleEdit=false;
                            //执行成功提示编辑成功信息.
                            if (response.data.flag){
                                this.$message.success(response.data.message);
                            }else {
                                this.$message.error(response.data.message);
                            }
                            //最后重新查询数据
                        }).finally(()=>{
                            this.findPage();
                        })
                        //表单验证失败直接 returnfalse结束.
                    }else {
                        this.$message.error("表单数据校验失败");
                        return false;
                    }
                })
            },
            //添加
            handleAdd () {
                //引用表单 通过validate方法判断表单校验是否成功
                this.$refs['dataAddForm'].validate((valid)=>{
                    //表单数据通过验证 发送ajax请求
                    if(valid){
                        axios.post("/menu/add.do?checkitemIds="+this.checkitemIds+"&checkitemIds1="+this.checkitemIds1,this.formData).then((response)=>{
                            //隐藏新增得窗口
                            this.dialogFormVisible=false;
                            //判断后端返回得fiag值
                            if(response.data.flag){
                                this.$message.success(response.data.message);
                            }else {
                                this.$message.error(response.data.message);
                            }
                        }).finally(()=>{
                            this.findPage();
                        })
                    }else {
                        this.$message.error("表单数据校验失败");
                        return false;
                    }
                })
            },
            //分页查询
            findPage() {
                //分页参数  从vue分页数据模型中获取参数.
                var param={
                    //当前页面
                    currentPage:this.pagination.currentPage,
                    //每页显示的数据数
                    pageSize:this.pagination.pageSize,
                    //查询条件.
                    queryString:this.pagination.queryString,
                }
                //请求后台  发送ajax请求 传递分页参数  当前页面  每页显示数  查询条件
                //返回数据为 后台页面返回类型.可调用返回类中的数据.
                axios.post("/menu/findPage.do",param).then((response)=>{
                    //给数据模型赋值 vue的双向绑定展示
                    this.dataList=response.data.rows;
                    //总记录数
                    this.pagination.total=response.data.total;
                })
            },
            // 重置表单
            resetForm() {
                this.formData={};
                this.checkitemIds=[];
                this.checkitemIds1=[];
            },
            // 弹出添加窗口
            handleCreate() {
                this.activeName = 'first';
                //先重置表单在显示窗口
                this.resetForm();
                this.dialogFormVisible = true;
                //发送ajax请求查询所有角色信息  所有子菜单信息
                axios.get("/menu/findAllRole.do").then((res) => {
                    if (res.data.flag) {
                        this.tableData = res.data.data;
                    } else {
                        this.$message.error(res.data.message)
                    }
                })
                axios.get("/menu/findAllmenu.do").then((res) => {
                    if (res.data.flag) {
                        this.tableData1 = res.data.data;

                    } else {
                        this.$message.error(res.data.message)
                    }
                })
            },
            // 弹出编辑窗口
            handleUpdate(row) {
                this.activeName = 'first';
                this.dialogFormVisibleEdit=true;
                //回显数据的两种方法.
                this.formData = JSON.parse(JSON.stringify(row));
               // this.checkitemIds= this.formData.roles.id;
                // 回显数据.
                axios.get("/menu/findAllRole.do").then((res) => {
                    if (res.data.flag) {
                        this.tableData = res.data.data;
                        //回显角色数据
                        axios.get("/menu/findRoleIdByMenuId.do?id="+row.id).then((res)=>{
                            if (res.data.flag){
                                this.checkitemIds=res.data.data;
                            }else {
                                this.$message.error(res.data.message);
                            }
                        })
                    } else {
                        this.$message.error(res.data.message)
                    }
                })
                axios.get("/menu/findAllmenu.do").then((res) => {
                    if (res.data.flag) {
                        this.tableData1 = res.data.data;
                        //回显子菜单数据
                        axios.get("/menu/findMenuIdByMenuId.do?id="+row.id).then((res)=>{
                            if (res.data.flag){
                                this.checkitemIds1=res.data.data;
                            }else {
                                this.$message.error(res.data.message);
                            }
                        })
                    } else {
                        this.$message.error(res.data.message)
                    }
                })

            },
            //切换页码
            handleCurrentChange(currentPage) {
                //参数为切换后的页码
                this.pagination.currentPage=currentPage;
                this.findPage();
            },
            // 删除
            handleDelete(row) {
                //弹出框  warning类型  点击确定后执行后续请求
                this.$confirm("你确定要删除当前数据么?","提示",{type:'warning'}).then(()=>{
                    axios.get("/menu/delete.do?id="+row.id).then((res)=>{
                        //删除成功
                        if (res.data.flag){
                            this.$message.success(res.data.message);

                        }else {
                            this.$message.error(res.data.message);
                        }
                        //调用分页,获取最新数据
                        this.findPage();
                    })
                })
            }
        }
    })
</script>
</html>